//
//  CunquHomeVC.m
//  ProjectforHDX
//
//  Created by yinda on 2018/6/8.
//  Copyright © 2018年 H.DX. All rights reserved.
//

#import "CunquHomeVC.h"
#import "OrderModel.h"
#import "CunquHomeHeadView.h" // 表头
#import "MainHandleTableCell.h" // 自定义cell

#import "CSNewOrderVC.h"   // 新建订单中心
#import "CSNewOrderLCVC.h"    // 新建订单LC
#import "CSNewOrderServiceVC.h"   // 新建客服
#import "CSNewOrderManageVC.h"
#import "CSNewOrderReplaceVC.h"

#import "MessageVC.h"  // 我的消息

#import "UserCenterVC.h" // 个人中心

#import "showDetailsView.h"

#import "THOpenView.h"

#import "MHScanViewController.h"

#import "MainHeader.h"

#import "DeliveryDetailsVC.h"

#import "CSMyOrderConfirmingTVC.h"//订单详情
#import "OrderDetailsVC.h"//订单详情

#import "CMyOrderVC.h"//我的订单

#import "WCheckgoodsVC.h"//物品盘点

#import "StockInquiryVC.h"//库存查询

#import "ShipmentReportVC.h" //已发货报告
#import "BDAlertView.h"
#import "CacheVC.h"
#import "PickupVC.h"
#import "CunjianDetailVC.h"

@interface CunquHomeVC ()<UITableViewDelegate, UITableViewDataSource,UIAlertViewDelegate> {
    BOOL  isMJRefresh;//是否可以刷新
    NSString *strKey;
}
@property (nonatomic, strong) NSMutableArray *tableArray;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *cabinetlistArr;  // 智能柜列表数组
@property (nonatomic, copy)NSString *types;  // 村取件类型

/**
 *  消息点
 */
@property (nonatomic, strong) RZCustomLable *labPoint;
/**
 *  表头视图
 */
@property (nonatomic, strong) CunquHomeHeadView *headerView;

@end
static NSString *mainTableCell_ID = @"mainHandleTableCell";
@implementation CunquHomeVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.tableArray=[[NSMutableArray alloc] initWithCapacity:0];
    THUserInfoModel *model = [THMangerTool getAccountInfo];
    if ([model.isVendor isEqualToString:@"1"]) {
        self.leftItem.hidden = YES;
        [self loggingOut];
    }
    [self.leftItem setImage:[UIImage imageNamed:@"base返回"] forState:UIControlStateNormal];
    self.rightItem.hidden=NO;
//    [self.rightItem setTitle:@"扫码" forState:0];
    [self.rightItem setImage:[UIImage imageNamed:@"scan2"] forState:UIControlStateNormal];
//    self.labPoint=[[RZCustomLable alloc] init];
//    [self.labPoint setFrame:CGRectMake(self.rightItem.width-15,6, 9, 9)];
//    [self.labPoint addRadius:self.labPoint.width/2.f borderwidth:0 bordercolor:nil];
//    [self.labPoint setBackgroundColor:[UIColor orangeColor]];
//    self.labPoint.layer.borderWidth = 1;
//    self.labPoint.layer.borderColor = SYSTEM_COLOR.CGColor;
//    [self.rightItem addSubview:self.labPoint];
    
    RZCustomImageView *logoView=[[RZCustomImageView alloc] init];
    UIImage *logo=BDFILEIMAGENAEM(@"Home/nav_logo");
    [logoView setFrame:CGRectMake(0, 0, logo.size.width, logo.size.height)];
    logoView.image=logo;
    self.customTitleView=logoView;
    
    [self setupViews];
    self.cabinetlistArr = [NSMutableArray new];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hahha) name:@"loginStatus" object:nil];
    [self createTHeaderView:nil];
}
// 厂商版的退出登录
- (void)loggingOut{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"厂商退出"] forState:UIControlStateNormal];
    //    [btn setTitle:@"中心" forState:0];
    btn.frame = CGRectMake(0, 0, 30, 20);
    [btn addTarget:self action:@selector(touchLogout) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    //使用弹簧控件缩小菜单按钮和边缘距离
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spaceItem.width = -10;
    self.navigationItem.leftBarButtonItems = @[spaceItem, barButtonItem];
}
#pragma mark 退出登录
- (void)touchLogout {
    
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:[UtilCheck language:@"setting.logout"] message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction *logoutAction = [UIAlertAction actionWithTitle:[UtilCheck language:@"setting.logout"] style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        NSMutableArray *arr = [NSMutableArray array];
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:arr forKey:@"getSearcHistoryKey"];
        [userDefaults synchronize];
        [THMangerTool removeAccount];
        [APPDELEGATESelf setUpMainViewControllerOrLoginViewController:NO];
    }];
    
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:[UtilCheck language:@"setting.cancel"] style:(UIAlertActionStyleCancel) handler:nil];
    [alertC addAction:logoutAction];
    [alertC addAction:cancleAction];
    if ([[UIDevice currentDevice].model isEqualToString:@"iPad"]) {
        alertC.popoverPresentationController.sourceView = self.view;
        alertC.popoverPresentationController.sourceRect = CGRectMake((self.view.frame.size.width-200)/2,self.view.frame.size.height,200,1.0);
    }
    [self presentViewController:alertC animated:YES completion:nil];
    
}

// 厂商用户强制选择智能柜
- (void)vendorSelectSmartCabinet{
    NSMutableArray *listmuArr = [NSMutableArray new];
    if (self.cabinetlistArr.count>0) {
        for (NSDictionary *dic in self.cabinetlistArr) {
            [listmuArr addObject:[dic objectForKey:@"code"]];
        }
    }
    if (listmuArr.count>0) {
        [BDAlertView showAlertWithTitle:[UtilCheck language:@"CunquHome.selectcabinet"] isChoice:YES otherTitles:listmuArr completion:^(BOOL cancelled, NSInteger selectedIndex) {
            if (selectedIndex==-1) {
                return ;
            }
            self.cabinetCode = listmuArr[selectedIndex];
            [[NSUserDefaults standardUserDefaults]setObject:listmuArr[selectedIndex] forKey:@"cabinetCode"];
            
        }];
    }
    else{
        [Toolobject showAlterwithMessage:[UtilCheck language:@"CunquHome.cabinet"]];
    }
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
//    self.types = @"1";
    isMJRefresh = YES;
    [self addHeader];
    
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc]init]];
    
    
    self.navigationController.navigationItem.rightBarButtonItem.enabled =NO;
    
    [self getMessage];
    [self getSmartCabinet];
   
}

- (void)hahha{
    NSLog(@"登录进来的");
    [self getHomepageDatas];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark 视图初始化
- (void)setupViews {
    
    self.view.backgroundColor = [Toolobject getColorByRGB:244 green:248 blue:251 alpha:1];
    // tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, BOUNDSWIDTH,self.view.height - 64) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UtilCheck getRZColor:244 green:248 blue:251 alpha:1];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[MainHandleTableCell class] forCellReuseIdentifier:mainTableCell_ID];
    [self.view addSubview:self.tableView];
}

#pragma mark- TableViewDelegate
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.tableArray.count;
}

#define CELL_HEIGHT 115
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderModel *orderModel=[self.tableArray objectAtIndex:indexPath.row];
    CGFloat cellHeight;
    if (orderModel.customer.length >15) {
        cellHeight = CELL_HEIGHT + 15;
    }
    else {
        cellHeight = CELL_HEIGHT;
    }
    if(orderModel.open){
        MainHandleTableCell *cell= [[MainHandleTableCell alloc] init];
        CGFloat heigt= cellHeight+[cell createStepView:orderModel].height;
        return heigt;
    }
    else{
        return cellHeight;
    }
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MainHandleTableCell *cell = (MainHandleTableCell*)[tableView dequeueReusableCellWithIdentifier:@"MainHandleTableCell"];
    if(cell == nil)
    {
        cell = [[MainHandleTableCell alloc] init];
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegateVC = self;
    cell.iscunquHome = YES;
    if ([_types isEqualToString:@"2"]) {
        cell.isqujian = YES;
    }
    OrderModel *model = [self.tableArray objectAtIndex:indexPath.row];
    NSLog(@"%@",model.time);
    cell.model=[self.tableArray objectAtIndex:indexPath.row];
    cell.Refesh=^(OrderModel* model){
        [self.tableArray setObject:model atIndexedSubscript:indexPath.row];
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        
    };
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    OrderModel *model = [self.tableArray objectAtIndex:indexPath.row];
    NSLog(@"订单类型是%@",model.type);
    if (model.orderType) {
//        if ([model.orderType integerValue] > 2) {
//            OrderDetailsVC *vc = [[OrderDetailsVC alloc]init];
//            vc.orderCode = model.code;
//            [self.navigationController pushViewController:vc animated:YES];
//        }
//        else {
//            CSMyOrderConfirmingTVC *view = [[CSMyOrderConfirmingTVC alloc]init];
//            view.orderCode = model.code;
//            [self.navigationController pushViewController:view animated:YES];
//        }
        CunjianDetailVC *vc = [[CunjianDetailVC alloc]init];
        vc.titlename = [self.types isEqualToString:@"2"] ? [UtilCheck language:@"order.qudetail"]:[UtilCheck language:@"order.cundetail"];
        vc.orderCode = model.code;
        vc.isQujian = [self.types isEqualToString:@"1"] ? @"0" : @"1";
        // 不要横屏
        if (![self.types isEqualToString:@"1"]) {
            vc.nohengping = YES;
        }
        [self.navigationController pushViewController:vc animated:YES];
        
    }else{
        [Toolobject showTextWithMessage:@"服务器返回的订单类型不存在"];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    
    
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
  
}

#pragma mark- 其他事件
/**
 *  左button的点击事件（进入个人中心）
 *
 *  @param sender
 */
-(void)leftItem:(RZCustomButtom *)sender{

    MainVC *vc = [[MainVC alloc] init];

    [self.navigationController pushViewController:vc animated:YES];
}

- (void)touchMessageButton:(UIButton *)sender {
    
    MessageListTVC *vc = [[MessageListTVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)rightItem:(RZCustomButtom *)sender{
    [self ScanQRcodelogin];
//    MessageVC *vc = [[MessageVC alloc]init];
//    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark- 网络请求
// 扫描二维码登录
- (void)ScanQRcodelogin{
    MHScanViewController *scanVC = [[MHScanViewController alloc]init];
    __weak typeof(self) weakSelf = self;
    scanVC.isdismiss = YES;
    scanVC.rebackData = ^(NSString *retStr){
        [AFHttpTool postJsonDatawithUrl:@"scanSmart" parameters:@{@"cabinetCode":retStr,@"key":[THMangerTool getKey]} success:^(id responseObject) {
            if(1==[[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue]){
                [Toolobject showAlterwithMessage:@"二维码登录成功"];
            }
            else{
                [Toolobject showAlterwithMessage:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
            }
        } fail:^(id responseObject, NSError *error) {
            [Toolobject showAlterwithMessage:@"扫描登录失败"];
        }];
    };
    CustomNavigationController *vc = [[CustomNavigationController alloc]initWithRootViewController:scanVC];
    [self presentViewController:vc animated:YES completion:^{
        
    }];
}
#pragma mark 添加下拉刷新
- (void)addHeader
{
    if(self.tableView.header==nil){
        __weak __typeof(self) weakSelf = self;
        // 添加上拉刷新头部控件0
        self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            if(isMJRefresh){
                isMJRefresh=NO;
                // 进入刷新状态后会自动调用这个block
                [weakSelf getHomepageDatas];
            }
        }];
        // 马上进入刷新状态
        [self.tableView.header beginRefreshing];
    }
    else
    {
        // 马上进入刷新状态
        [self.tableView.header beginRefreshing];
    }
}

- (void)getHomepageDatas {
//    [self requestOrdersStatistical];
    [self requestData];
}


/**
 *获取消息数据
 */
- (void)getMessage{
    
    [[BDHUD sharedManager] showBDHUD:self.view title:[THMangerTool language:@"HUD.title"]];
    
    [AFHttpTool postJsonDatawithUrl:@"messagesOrderBy" parameters:nil success:^(id responseObject) {
        [[BDHUD sharedManager] dismissHUD];
        if ([[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue] == 1) {
            NSArray *array = [responseObject objectForKey:@"data"];
            int unreadCount = 0;
            for (NSDictionary *dic in array) {
                if ([[dic objectForKey:@"unRead"] integerValue] > 0) {
                    unreadCount ++;
                }
            }
            if (unreadCount >0) {
                self.labPoint.hidden = NO;
            }else{
                self.labPoint.hidden = YES;
            }
        }else {
            
            [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
        }
        
    } fail:^(id responseObject, NSError *error) {
        
        [[BDHUD sharedManager] dismissHUD];
        
        if ([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:@"请求超时。"]) {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.time.out"]];
        }else {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.network.interruption"]];
        }
    }];
    
}

/**
 *  获取当前智能柜列表
 */
- (void)getSmartCabinet{
    [AFHttpTool postJsonDatawithUrl:@"getSmartCabinet" parameters:nil
                            success:^(id responseObject) {
                                if(1==[[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue]){
                                    self.cabinetlistArr = [NSMutableArray new];
                                    [self.cabinetlistArr addObjectsFromArray:[responseObject objectForKey:@"data"]];
                                    THUserInfoModel *model = [THMangerTool getAccountInfo];
                                    if ([model.isVendor isEqualToString:@"1"]&&self.cabinetCode==nil) {
                                        // 强制厂商选择智能柜
                                        [self vendorSelectSmartCabinet];
                                    }
                                }
                                
                            } fail:^(id responseObject, NSError *error) {
                                NSLog(@"获取智能柜列表失败");
                                [self createTHeaderView:nil];
                                
                            }];
}


//创建或者修改头
-(void)createTHeaderView:(NSArray*)arr{
    
    if(!arr){
        
        arr=@[@{@"type":@"1",@"typeName":@"未完成订单",@"count":@"0"},@{@"type":@"2",@"typeName":@"待发货",@"count":@"0"},@{@"type":@"3",@"typeName":@"待收货",@"count":@"0"},@{@"type":@"4",@"typeName":@"待返件",@"count":@"0"}];
    }
    
    
    self.headerView = [[CunquHomeHeadView alloc] initHeaderView:arr];
    __weak __typeof__(self) weadSelf=self;
    self.headerView.viewblock=^(UICustomButtonType type){
        [weadSelf pushViewByType:type];
    };
    self.tableView.tableHeaderView=self.headerView;
    
    if(!self.headerView){
        
        
    }
    else{
        [self.headerView setValueArray:arr];
    }
    
}


/**
 *  获取首页订单数据
 */
-(void)requestData{
    [[BDHUD sharedManager] dismissHUD];
    if (self.types==nil) {
        self.types = @"1";
    }
    NSDictionary *post=[NSDictionary dictionaryWithObjectsAndKeys:@"20",@"size",self.types,@"type",@"1",@"page", nil];
    [AFHttpTool postJsonDatawithUrl:@"getAccessList" parameters:[post mutableCopy]
                            success:^(id responseObject) {
                                isMJRefresh=YES;
                                [self.tableView.header endRefreshing];
                                [[BDHUD sharedManager] dismissHUD];
                                if(1==[[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue]){
                                    if([[responseObject objectForKey:@"data"] isKindOfClass:[NSArray class]]){
                                        NSArray *data=[responseObject objectForKey:@"data"];
                                        self.tableArray=[UtilCheck arraytoModel:data targetModel:@"OrderModel"];
                                    
                                        
                                        //                                        if(self.tableArray.count<1){
                                        //                                            [[BDHUD sharedManager] showNOneDataViewHUD:self.tableView top:120 title:[THMangerTool language:@"HUD.noOrderData"] onClick:^{
                                        //                                                  [self addHeader];
                                        //                                            }];
                                        //                                        }
                                        
                                        [self.tableView reloadData];
                                    }
                                    else{
                                        [self.tableArray removeAllObjects];
                                        if(self.tableArray.count<1){
                                            [[BDHUD sharedManager] showNOneDataViewHUD:self.tableView top:120 title:[THMangerTool language:@"HUD.noOrderData"] onClick:^{
                                                [self addHeader];
                                            }];
                                        }
                                        [[BDHUD sharedManager] showWords:[THMangerTool language:@"HUD.nothing"]];
                                       
                                    }
                                }
                                else{
                                    //返回数据出问题 弹出服务器返回信息
                                    if([[[responseObject objectForKey:@"head"] allKeys] containsObject:@"info"]&&![UtilCheck DataIsNuLL:[[responseObject objectForKey:@"head"] objectForKey:@"info"]]){
                                        [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
                                    }
                                    else{
                                        [[BDHUD sharedManager] showWords:[THMangerTool language:@"HUD.nothing"]];
                                    }
                                    [self.tableArray removeAllObjects];
                                    if(self.tableArray.count<1){
                                        [[BDHUD sharedManager] showNOneDataViewHUD:self.tableView top:120 title:[THMangerTool language:@"HUD.noOrderData"] onClick:^{
                                            [self addHeader];
                                        }];
                                        
                                    }
                                }
                            } fail:^(id responseObject, NSError *error) {
                                isMJRefresh=YES;
                                [self.tableView.header endRefreshing];
                                [[BDHUD sharedManager] dismissHUD];
                                if ([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:@"请求超时。"]) {
                                    [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.time.out"]];
                                }else {
                                    [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.network.interruption"]];
                                }
                            }];
    
}


#pragma mark 界面跳转
-(void)pushViewByType:(UICustomButtonType)type {
    THUserInfoModel *userModel0=[THMangerTool getAccountInfo];
    NSLog(@"usertype是%@   servicePermissions是%@",userModel0.userType,userModel0.servicePermissions);
    NSMutableArray *listmuArr = [NSMutableArray new];
    if (self.cabinetlistArr.count>0) {
        for (NSDictionary *dic in self.cabinetlistArr) {
            [listmuArr addObject:[dic objectForKey:@"code"]];
        }
    }
    switch (type) {
        case NSButtonCunjianNice ://存件
        {
                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"开箱提示" message:@"确定要开箱存好件?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                alertView.tag = 201808201;
                [alertView show];
            
        }
         break;
        case NSButtonQujianNice ://取好件
        {
                NSString *type = @"1";
                AppDelegate * app = (AppDelegate *)[UIApplication sharedApplication].delegate;
                app.shouldChangeOrientation = YES;
                PickupVC *vc = [[PickupVC alloc]init];
                vc.type = type;
                vc.quCabinetCode = self.cabinetCode;
                //                [self presentViewController:vc animated:YES completion:nil];
                [self.navigationController pushViewController:vc animated:YES];
            
        }
        break;
            
        case NSButtonCunjianBad ://存故障板件
        {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"开箱提示" message:@"确定要开箱存故障件?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                alertView.tag = 201808202;
                [alertView show];
//                NSString *type =  @"2";
//                // 打开箱门
//                [[BDHUD sharedManager] showBDHUD:self.view title:[THMangerTool language:@"HUD.title"]];
//                [AFHttpTool postJsonDatawithUrl:@"openBoxBySave" parameters:@{@"type":type,@"cabinetCode":listmuArr[selectedIndex]}
//                                        success:^(id responseObject) {
//                                            [[BDHUD sharedManager] dismissHUD:self.view];
//                                            if(1==[[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue]){
//                                                [[BDHUD sharedManager]showWords:[UtilCheck language:@"cunquDetail.Openedsuccessfully"]];
//                                                CacheVC *vc = [[CacheVC alloc]init];
//                                                vc.cabinetCode = listmuArr[selectedIndex];
//                                                vc.boxcode = [[responseObject objectForKey:@"data"]objectForKey:@"boxCode"]; // 箱号
//                                                vc.boxID = [[responseObject objectForKey:@"data"]objectForKey:@"boxID"]; // 箱号ID
//                                                vc.freenumber = [[responseObject objectForKey:@"data"]objectForKey:@"number"]; // 空余数量
//                                                vc.serialNumber = [[responseObject objectForKey:@"data"]objectForKey:@"serialNumber"]; // 流水号
//                                                vc.type = type;
//                                                [self.navigationController pushViewController:vc animated:YES];
//                                            }
//                                            else{
//                                                NSLog(@"%@",[responseObject objectForKey:@"head"][@"info"]);
//                                                [[[UIAlertView alloc]initWithTitle:[UtilCheck language:@"CachVC.kaixaingtishi"] message:[responseObject objectForKey:@"head"][@"info"] delegate:self cancelButtonTitle:[UtilCheck language:@"show.gotIt"] otherButtonTitles:nil, nil]show];
//                                            }
//                                        } fail:^(id responseObject, NSError *error) {
//                                            [[BDHUD sharedManager] dismissHUD:self.view];
//                                            NSLog(@"打开箱门失败");
//                                            [[[UIAlertView alloc]initWithTitle:[UtilCheck language:@"CachVC.kaixaingtishi"] message:[responseObject objectForKey:@"head"][@"info"] delegate:self cancelButtonTitle:[UtilCheck language:@"show.gotIt"] otherButtonTitles:nil, nil]show];
//
//                                        }];
            
        }
            break;
        case NSButtonQujianBad ://取故障件
        {
            NSString *type = @"2";
            AppDelegate * app = (AppDelegate *)[UIApplication sharedApplication].delegate;
            app.shouldChangeOrientation = YES;
            PickupVC *vc = [[PickupVC alloc]init];
            vc.type = type;
            vc.quCabinetCode = self.cabinetCode;
            //                [self presentViewController:vc animated:YES completion:nil];
            [self.navigationController pushViewController:vc animated:YES];
            
        }
            break;
        case NSButtonCunjianList ://存件列表
        {
            self.types = @"1";
            [self addHeader];
    
        }
           break;
            
        case NSButtonQujianList ://取件列表
        {
            self.types = @"2";
            [self addHeader];
        }
            break;
        default:
            break;
    }
}
#pragma mark 确认存件提示
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        return;
    }
    NSString *type;
    if (alertView.tag==201808202) {
        type =  @"2";
    }
    else{
        type =  @"1";
    }
        // 打开箱门
        [[BDHUD sharedManager] showBDHUD:self.view title:[THMangerTool language:@"HUD.title"]];
        [AFHttpTool postJsonDatawithUrl:@"openBoxBySave" parameters:@{@"type":type,@"cabinetCode":self.cabinetCode}
                                success:^(id responseObject) {
                                    [[BDHUD sharedManager] dismissHUD:self.view];
                                    if(1==[[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue]){
                                        CacheVC *vc = [[CacheVC alloc]init];
                                        vc.cabinetCode = self.cabinetCode;
                                        vc.boxcode = [[responseObject objectForKey:@"data"]objectForKey:@"boxCode"]; // 箱号
                                        vc.boxID = [[responseObject objectForKey:@"data"]objectForKey:@"boxID"]; // 箱号ID
                                        vc.freenumber = [[responseObject objectForKey:@"data"]objectForKey:@"number"]; // 空余数量
                                        vc.serialNumber = [[responseObject objectForKey:@"data"]objectForKey:@"serialNumber"]; // 流水号
                                        vc.type = type;
                                         [[BDHUD sharedManager]showWords:[NSString stringWithFormat:[UtilCheck language:@"cunquDetail.Openedsuccessfully"],vc.boxcode]];
                                        [self.navigationController pushViewController:vc animated:YES];
                                    }
                                    else{
                                        NSLog(@"%@",[responseObject objectForKey:@"head"][@"info"]);
                                        [[[UIAlertView alloc]initWithTitle:[UtilCheck language:@"CachVC.kaixaingtishi"] message:[responseObject objectForKey:@"head"][@"info"] delegate:self cancelButtonTitle:[UtilCheck language:@"show.gotIt"] otherButtonTitles:nil, nil]show];
                                    }
                                } fail:^(id responseObject, NSError *error) {
                                    [[BDHUD sharedManager] dismissHUD:self.view];
                                    NSLog(@"打开箱门失败");
                                    [[[UIAlertView alloc]initWithTitle:[UtilCheck language:@"CachVC.kaixaingtishi"] message:[responseObject objectForKey:@"head"][@"info"] delegate:self cancelButtonTitle:[UtilCheck language:@"show.gotIt"] otherButtonTitles:nil, nil]show];
                                    
                                }];
}

@end
